package com.fc.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fc.config.AutoGenerateCodeUtil;
import com.fc.dto.CodeGenarateDTO;
import com.fc.dto.FieldDto;
import com.fc.dto.TableDTO;
import com.fc.entity.SysPageSet;
import com.fc.entity.SysPermission;
import com.fc.entity.SysTable;
import com.fc.mapper.CodeGenerateMapper;
import com.fc.service.CodeGenerateService;
import com.fc.service.SysPageSetService;
import com.fc.service.SysPermissionService;
import com.fc.service.SysTableService;
import com.fc.vo.ColumnVO;
import com.fc.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class CodeGenerateServiceImpl implements CodeGenerateService {
    @Resource
    private CodeGenerateMapper codeGenerateMapper;

    @Autowired
    private SysTableService sysTableService;

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private AutoGenerateCodeUtil autoGenerateCodeUtil;

    @Autowired
    private SysPageSetService sysPageSetService;

    @Value("${zf.database}")
    private String database;

    @Override
    public List<TableVO> getList(Page<TableVO> page, TableDTO dto) {
        if(StrUtil.isEmpty(dto.getDatabase())){
            dto.setDatabase(database);
        }
        return codeGenerateMapper.selectList(page,dto);
    }

    @Override
    public void generate(String id) {
        SysTable sysTable = sysTableService.getById(id);
        CodeGenarateDTO dto  = new CodeGenarateDTO();
        dto.setAuthor("zffc");
        dto.setDbType("mysql");
        dto.setInclude(sysTable.getTableName());
        dto.setParent("com.fc");
        dto.setModuleName(null);
        loadConition(dto,sysTable.getId());
        autoGenerateCodeUtil.autoGenerateCode(dto);
        //保存菜单信息
//        saveMenu(sysTable);
    }

    private void saveMenu(SysTable sysTable){
        SysPermission sysPermission = new SysPermission();
        sysPermission.setMenuType(1);
        sysPermission.setName(sysTable.getDescription());
        sysPermission.setUrl("/"+StrUtil.toCamelCase(sysTable.getTableName()+"/toIndex"));
        sysPermissionService.save(sysPermission);
    }

    private void loadConition(CodeGenarateDTO dto,String tableId){
        List<SysPageSet> query = sysPageSetService.getPageSetByTable(tableId,1);
        List<FieldDto> queryfc = BeanUtil.copyToList(query,FieldDto.class);
        dto.setQuery(queryfc);
        List<SysPageSet> list = sysPageSetService.getPageSetByTable(tableId,2);
        List<FieldDto> listfc =  BeanUtil.copyToList(list,FieldDto.class);
        dto.setList(listfc);
        List<SysPageSet> form = sysPageSetService.getPageSetByTable(tableId,3);
        List<FieldDto> formfc = BeanUtil.copyToList(form,FieldDto.class);
        dto.setForm(formfc);
    }


    @Override
    public List<ColumnVO> getColumnByName(String tableName) {
        List<ColumnVO> list = codeGenerateMapper.selectColumnByName(tableName);
        //驼峰转换
        list.forEach( e -> e.setField(StrUtil.toCamelCase(e.getField())));
        return list;
    }
}
